<?php
namespace app\console\controller;



//公共ajax调用控制器
use think\facade\Request;
use function Symfony\Component\Translation\t;
use think\facade\Db;
class Common extends Base
{
    //验证字段是否存在--解决所有表和字段的验证
	public function validonly()
	{
		$valid = true;
		if (Request::instance()->isPOST())
		{ 
			$table = Request::instance()->param('table');//使用传来的表名

			$param = Request::instance()->except(['id'],'post');

			$param['id'] = ['not in',Request::instance()->post('id')];
			
			$isexist = db::name($table)->field('id')->where($param)->find();
			if($isexist){
				 $valid = false;
			}
		}
		else{
            $valid = false;
		}
		$data['valid'] = $valid;
		echo json_encode($data);
		exit;
	}
    //上传图片
	public function upload(){
        $module = app('http')->getName();
        $file = Request::file('file_data');
        $param = Request::param();
        if(Request::action() == 'batch_create'){
            $savename = \think\facade\Filesystem::disk('public')->putFileAs($module, Request::file('multi_pic'),$file->getOriginalName());
            return json_encode(['code' => 0,'img' =>  $savename]);
        }else{

            if(!empty($param['rule'])){
                return json_encode(['code' => 0,'img' =>  uploadOneOrigin('file_data')]);
            }else{
                return json_encode(['code' => 0,'img' =>  uploadOne('file_data')]);
            }

        }



	}

	//删除图片
	public function updel(){
		$filename = input('filename');
			if (!empty($filename)) {
				@unlink(root_path() . 'public'. DS .$filename);
				$data['code']=1;
			} else {
				$data['code']=2;
			}
		return json_encode($data);

	}
    /**
     * 配置管理头部排序
     * @return [type] [description]
     */
	public function sortable(){
		if (Request::instance()->isPOST())
		{ 
			$sort = Request::instance()->post('sort/a');//使用传来的表名
			for($i=0;$i<count($sort);$i++){
				Db::name('webtype')->where('id', $sort[$i])->update(['orderid' => ($i+1)]);
			}
			$data['code']=1;
		}
		else{
            $data['code']=0;
		}
		echo json_encode($data);
		exit;

	}
    /**
     * 配置管理内容排序
     * @return [type] [description]
     */
	public function sortConfig(){
		if (Request::instance()->isPOST())
		{ 
			$sort = Request::instance()->post('sort/a');//使用传来的表名
			for($i=0;$i<count($sort);$i++){
				Db::name('webconfig')->where('varname', $sort[$i])->update(['orderid' => ($i+1)]);
			}
			$data['code']=1;
		}
		else{
            $data['code']=0;
		}
		echo json_encode($data);
		exit;

	}
    /**
     * 更新配置内容
     * @return [type] [description]
     */
	public function editable(){
		$data = Request::instance()->param();
		Db::name('webconfig')->where('varname', $data['pk'])->update(['varvalue' => $data['value']]);
        $value = Db::name('webconfig')->field('varname,varvalue')->select();
        $arr = [];
        foreach($value as $v){
            $arr[$v['varname']] = $v['varvalue'];
        }
		// 文件路径
        $path = root_path() . "common" . DS . "common" . DS . "web.inc.php";
        file_put_contents($path, serialize($arr));
        return true;
	}

	public function getRegion(){

		if (Request::instance()->isPOST())
		{

			$pcode = Request::post('pcode');//父编码
			$level = Request::post('level');//等级
			if($level==1){
                $data['list'] = Db::name('cn_city')->field('value,code,name')->where('pcode',$pcode)->select();
			}else{
				$data['list'] = Db::name('cn_area')->where('status',1)->field('value,code,name')->where('pcode',$pcode)->select();
			}

			$data['code']=1;
		}
		else{
            $data['code']=0;
		}
		echo json_encode($data);
		exit;

	}

}